﻿using Demo.PetaPoco.Application.Dtos.Area;
using Demo.PetaPoco.Application.Dtos.Vue;

namespace Demo.PetaPoco.Application.Extensions;

/// <summary>
/// AreaDto 的扩展方法
/// </summary>
public static class AreaDtoExtensions
{
	/// <summary>
	/// 将 IEnumerable<AreaDto> 转换为 IEnumerable<OptionItem>，递归处理子节点
	/// </summary>
	/// <param name="areas">要转换的 AreaDto 集合</param>
	/// <returns>返回对应的 OptionItem 集合</returns>
	public static IEnumerable<OptionItem> ToOptionItems(this IEnumerable<IAreaDto> areas)
	{
		return areas.Select(area => ConvertToOptionItem(area)).ToList();
	}

	/// <summary>
	/// 将单个 AreaDto 转换为 OptionItem，并递归处理子节点
	/// </summary>
	/// <param name="area">当前 AreaDto 节点</param>
	/// <returns>返回对应的 OptionItem</returns>
	private static OptionItem ConvertToOptionItem(IAreaDto area)
	{
		var optionItem = new OptionItem
		{
			Label = area.Name,
			Value = area.Id.ToString(),
			Children = new List<OptionItem>()
		};

		// 递归转换子节点
		if (area.Children != null && area.Children.Any())
		{
			optionItem.Children = area.Children.Select(child => ConvertToOptionItem(child)).ToList();
		}

		return optionItem;
	}
}
